﻿'This  class will receive the ArrayList and action variables and make necessary modifications to database and to 
'Application Variables. 
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient


Public Class RuleActions
    Private cardAttribute As ArrayList
    Private action As String

    'custructor
    Public Sub New(attributeArray As ArrayList, actionName As String)
        cardAttribute = New ArrayList()
        cardAttribute = attributeArray
        action = actionName
        
    End Sub
    'Make an action
    Public Sub makeAction(ajaxGEorig As ajaxGameEngine, cardRule1 As CardRule, systemID As Integer)
        Dim index As Integer = 0
        Dim ajaxGE As ajaxGameEngine = ajaxGEorig
        Dim cardRulePassed As CardRule = cardRule1
        Dim connectLink As DBConnect = New DBConnect()
        Dim SqlCmd As SqlCommand = New SqlCommand
        Dim cardArrayInPlay As ArrayList
        Dim cardsForUser As UserCards

        'Enter
        If action = "enter" Then
            ' MsgBox("This is Enter Action")
            'Apply elements from array to the system attribute values
            For index = 0 To cardRulePassed.cardAttribsEnterPlay.Count - 1
                Dim attrib As CardAttributes = New CardAttributes()
                attrib = cardRulePassed.cardAttribsEnterPlay(index)
                ajaxGE.modifySysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
            Next
            'Insert Card into MatchPlayer_Card table

            SqlCmd.CommandText = "INSERT INTO MatchPlayer_Card (MatchID, ParticipantID, CardID) VALUES ('" & ajaxGE.Session("matchID").ToString & "', '" _
                & ajaxGE.userID.participantID & "', '" & cardRule1.cardID.ToString & "')"
            connectLink.doUpdate2(SqlCmd.CommandText)
            'add card for the user
            cardsForUser = New UserCards(cardRule1.cardID.ToString, ajaxGE.userID.participantID.ToString)
            cardArrayInPlay = ajaxGE.Application("cardArray")
            cardArrayInPlay.Add(cardsForUser)
            'save it in application variable

        End If
        'Leave
        If action = "leave" Then
            ' MsgBox("This is Leave Action")
            'Apply elements from array to the system attribute values
            For index = 0 To cardRulePassed.cardAttribsLeavePlay.Count - 1
                Dim attrib As CardAttributes = New CardAttributes()
                attrib = cardRulePassed.cardAttribsLeavePlay(index)
                ajaxGE.modifySysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
            Next
            'Insert Card into MatchPlayer_Card table

            SqlCmd.CommandText = "DELETE FROM MatchPlayer_Card WHERE MatchID = '" & ajaxGE.Session("matchID").ToString & "' AND ParticipantID = '" _
                & ajaxGE.userID.participantID & "' AND CardID = '" & cardRule1.cardID & "'"
            connectLink.doUpdate2(SqlCmd.CommandText)
            'add card for the user
            ajaxGE.cardArray.Remove("cardIDtxt.Text")
            'save it in application variable
            ajaxGE.Application("cardArray") = ajaxGE.cardArray
        End If
        'Add
        'If action = "add" Then
        '    ' MsgBox("This is Add Action")
        '    'Print out from array ADD
        '    For index = 0 To cardRulePassed.cardAttribsAdd.Count - 1
        '        Dim attrib As CardAttributes = New CardAttributes()
        '        attrib = cardRulePassed.cardAttribsAdd(index)
        '        ajaxGE.modifySysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
        '    Next
        'End If
        ''Remove
        'If action = "remove" Then
        '    ' MsgBox("This is remove action")
        '    'Apply elements from array to the system attribute values
        '    For index = 0 To cardRulePassed.cardAttribsRemove.Count - 1
        '        Dim attrib As CardAttributes = New CardAttributes()
        '        attrib = cardRulePassed.cardAttribsRemove(index)
        '        ajaxGE.modifySysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
        '    Next
        'End If
        'Remain In Play
        If action = "remain" Then
            ' MsgBox("This is remove Remain-in-game")
            'Apply elements from array to the system attribute values
            For index = 0 To cardRulePassed.cardAttribsCheckRemain.Count - 1
                Dim attrib As CardAttributes = New CardAttributes()
                attrib = cardRulePassed.cardAttribsCheckRemain(index)
                ajaxGE.modifySysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
            Next
        End If
        'Check in Remain
        If action = "checkIn" Then
            ' MsgBox("This is remove Check Values of the cards")
            'Apply elements from array to the system attribute values
            For index = 0 To cardRulePassed.cardAttribsCheckRemain.Count - 1
                Dim attrib As CardAttributes = New CardAttributes()
                attrib = cardRulePassed.cardAttribsCheckRemain(index)
                ajaxGE.checkSysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
            Next
        End If
        'Required to play
        If action = "required" Then
            ' MsgBox("This is required action")
            'Apply elements from array to the system attribute values
            For index = 0 To cardRulePassed.cardAttribsRequiredToPlay.Count - 1
                Dim attrib As CardAttributes = New CardAttributes()
                attrib = cardRulePassed.cardAttribsRequiredToPlay(index)
                'Check Item and see if the system attributes have enough value in order to play
                ajaxGE.checkSysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
            Next
        End If
    End Sub


End Class
